
import axios from 'axios'; // 确保已安装并导入axios

export default async function getWMSInfo(event, map, layerName) {
    const source = layerName.getSource();
    const viewResolution = map.getView().getResolution();
    const url = source.getFeatureInfoUrl(
        event.coordinate,
        viewResolution,
        'EPSG:3857',
        {'INFO_FORMAT': 'application/json', 'FEATURE_COUNT': 1}
    );

    if (!url) return null; // 无URL时直接返回null

    try {
        const response = await axios.get(url, {
            responseType: 'json', // 确保获取原始文本（与response.text()行为一致）
            timeout: 5000,      // 可选：添加请求超时
        });
        console.log('Feature Info:', response.data);
        return response.data; // 返回实际数据
    } catch (error) {
        console.error('Error fetching feature info:', error);
        throw error; // 抛出错误供调用方处理
    }
}